package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.dao.AuctOrgDao;
import com.zis.auction.model.AuctOrg;
import com.zis.auction.service.IAuctOrgInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2017/12/26
 */
@Service
@Transactional("txManager")
public class AuctOrgInterImpl extends BaseInterImpl<AuctOrg> implements IAuctOrgInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private AuctOrgDao auctOrgDao;

    @Override
    public String insert(AuctOrg auctOrg) throws Exception {
        auctOrg.setCreateDate(new Date());
        int num = sqlManager.insert(auctOrg);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(AuctOrg auctOrg) throws Exception {
        auctOrg.setUpdateDate(new Date());
        int num = sqlManager.updateTemplateById(auctOrg);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(AuctOrg.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public AuctOrg selectById(String id) throws Exception {
        return sqlManager.unique(AuctOrg.class, id);
    }

    @Override
    public List<AuctOrg> findByCondition(AuctOrg auctOrg) throws Exception {
        if(auctOrg==null){
            return sqlManager.all(AuctOrg.class);
        }
        return sqlManager.template(auctOrg);
    }

    @Override
    public List<JSONObject> findOptions(String type) throws Exception {
        List<JSONObject> options = new ArrayList<>();
        List<AuctOrg> orgList = auctOrgDao.findOptions(type);
        for (AuctOrg org : orgList) {
            JSONObject option = new JSONObject();
            option.put("value", org.getAuoId());
            option.put("label", org.getOrgName());
            options.add(option);
        }
        return options;
    }

    @Override
    public List<JSONObject> findRecom() throws Exception {
        List<JSONObject> dataList = new ArrayList<>();
        List<AuctOrg> orgList = auctOrgDao.findRecom();
        for (AuctOrg org : orgList) {
            JSONObject option = new JSONObject();
            option.put("orgId", org.getAuoId());
            option.put("orgName", org.getOrgName());
            option.put("orgHead", org.getOrgHead());
            option.put("orgTel", org.getOrgTel());
            option.put("orgImg", org.getTails().get("orgImgShow"));
            dataList.add(option);
        }
        return dataList;
    }
}
